<template>
	<view>
		<uni-section title="学生姓名" sub-title="" type="square">
			<view> {{data.name}} </view>
		</uni-section>

		<uni-section title="学生密码" sub-title="" type="square">
			<view> {{data.password}} </view>
		</uni-section>

		<uni-section title="学生邮箱" sub-title="" type="square">
			<view> {{data.email}} </view>
		</uni-section>

		<uni-section title="学生电话" sub-title="" type="square">
			<view> {{data.telephone}}</view>
		</uni-section>

		<uni-section title="学生学院" sub-title="" type="square">
			<view>{{data.department}}</view>
		</uni-section>

		<uni-section title="学生专业" sub-title="" type="square">
			<view>{{data.major}}</view>
		</uni-section>

		<uni-section title="最新评论" sub-title="" type="square">
			<view v-for="(item,index) in commentList" :key="index" v-if="index<3">{{item.content}}</view>
		</uni-section>
		<br />

		<view class="">
			<button type="default" @click="showPostDialog">发表评论</button><br />
			<navigator :url="'../comment/comment?id='+data.id" open-type="navigate"><button type="default">查看评论</button>
			</navigator>
		</view>

		<uni-popup ref="popup" type="center">
			<uni-popup-dialog title="学生评论发表" @confirm="postMessage">
				<view style="width: 100%;">
					<view>总体满意度</view>
					<uni-rate value="0"></uni-rate>
					<uni-easyinput v-model="message" placeholder="请输入评论内容" type="textarea" />
				</view>
			</uni-popup-dialog>
		</uni-popup>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				message: '',
				data: {
					"id": 1,
					"name": "张三",
					"password": "123",
					"email": "zs@163.com",
					"telephone": "15116785432",
					"department": "信息工程学院",
					"major": "移动应用开发"
				},
				id: 0,
				commentList: []
			}
		},
		methods: {
			postMessage() {
				let data = {
					"content": this.message,
					"id": this.data.id,
					"date": new Date().toISOString().substring(0.10),
					"name": this.data.name,
					"title": this.message
				}
				uni.request({
					url: 'http://localhost:8090/freshmenapp/message/addone',
					method: 'POST',
					data: data,
					success: res => {
						if (res.data.success) {
							this.data = res.data.data
							uni.showToast({
								title: '成功'
							});
						} else {
							uni.showToast({
								title: '失败',
								icon: 'none'
							});
						}
					},
					fail: () => {
						uni.showToast({
							title: '网络错误',
							icon: 'none'
						});
					},
					complete: () => {}
				});
			},

			showPostDialog() {
				this.$refs.popup.open()
			},
			reload() {
				uni.request({
					url: 'http://localhost:8090/freshmenapp/message/list',
					method: 'POST',
					data: {
						id: this.id
					},
					success: res => {
						this.commentList = [...res.data.list].reverse()
						this.id++
					},
					fail: () => {},
					complete: () => {
						uni.stopPullDownRefresh()
					}
				});
			},

		},
		onLoad(opt) {
			uni.request({
				url: 'http://localhost:8090/freshmenapp/classmate/findone',
				method: 'POST',
				data: {
					"id": opt.id
				},
				success: res => {
					if (res.data.success) {
						this.data = res.data.data
						uni.showToast({
							title: '成功'
						});
					} else {
						uni.showToast({
							title: '失败',
							icon: 'none'
						});
					}
				},
				fail: () => {
					uni.showToast({
						title: '网络错误',
						icon: 'none'
					});
				},
				complete: () => {}
			})
			this.reload()
		}
	}
</script>

<style>
	.uni-section view {
		margin-left: 22px;
	}
</style>